package com.amit.jdbc.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import java.sql.Statement;
import java.util.Scanner;

public class Lab8 {

	public static void main(String[] args) throws Exception {
		
		Connection con=null;

		try {
			//1.Load the driver class
			Class.forName("com.mysql.jdbc.Driver");
			//Class.forName("oracle.jdbc.driver.OracleDriver");
			
			//2.create the connection
			
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdb","root","root");
			//Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","root","root");
			
			//3.create the sql query
			
			Scanner sc=new Scanner(System.in);
			
			System.out.println("Enter the query");
			String qry=sc.nextLine();
			
			//4.create the statement object
			
			Statement st=con.createStatement();
			
			//5.submit the jdbc statement
			
			boolean b=st.execute(qry);
			
			//6.process the result
			
			if(b)
			{
				ResultSet rs=st.getResultSet();
				if(rs.next())
				{
					do
					{
						int sid=rs.getInt(1);
						String nm=rs.getString(2);
						String eml=rs.getString(3);
						long ph=rs.getLong(4);
						System.out.println(sid+" "+nm+" "+eml+" "+ph);
					}
					while(rs.next());
				}
			}
			else
			{
				int x=st.getUpdateCount();
				System.out.println("Result:"+x);
			}	
			
		} catch (Exception e) {
			
			e.printStackTrace();
			con.close();
		}
	}
}
